<?php

require_once( 'mvc/mvc-exception.php' );

require_once( 'flexical/model/feed-query.php' );
require_once( 'flexical/model/listing.php' );

require_once( 'flexical/admin/object-builder.php' );
require_once( 'flexical/admin/request-factory.php' );
require_once( 'flexical/admin/listing-request.php' );

class Flexical_Admin_Listing_Request_Factory extends Flexical_Admin_Request_Factory
{
	public function get_request()
	{
		$request = null;

		switch( $this->get_request_type( $_REQUEST ) )
		{
			case 'show_listings':
				$request = new Flexical_Admin_Show_Listings_Request();
				break;

			case 'create_listing':
				$request = new Flexical_Admin_Create_Listing_Request( $this->build_listing( $_REQUEST ), $this->get_cancel_selected( $_REQUEST ) );
				break;

			case 'edit_listing':
				$request = new Flexical_Admin_Edit_Listing_Request( $this->build_listing( $_REQUEST ) );
				break;

			case 'update_listing':
				$request = new Flexical_Admin_Update_Listing_Request( $this->build_listing( $_REQUEST ), $this->get_cancel_selected( $_REQUEST ) );
				break;

			case 'confirm_delete_listing':
				$request = new Flexical_Admin_Confirm_Delete_Listing_Request( $this->build_listing( $_REQUEST ) );
				break;

			case 'delete_listing':
				$request = new Flexical_Admin_Delete_Listing_Request( $this->build_listing( $_REQUEST ), $this->get_cancel_selected( $_REQUEST ) );
				break;

			case 'delete_all_listings':
				$request = new Flexical_Admin_Delete_All_Listings_Request( $this->get_confirmation( $_REQUEST ) );
				break;

			case 'test_listing':
				$request = new Flexical_Admin_Test_Listing_Request( $this->build_listing( $_REQUEST ) );
				break;

			default:
				throw new Exception( "Unknown request type: " . $this->get_request_type( $_REQUEST ) );
		}

		return $request;
	}

	private function build_listing( $request_data )
	{
		$listing = $this->build_listing_object( $request_data );
		$feed_query_count = $this->get_feed_query_count( $request_data );

		if ( $feed_query_count < 0 )
		{
			throw new Exception( "Invalid feed query count: " . $feed_query_count );
		}

		for ( $i = 0; $i < $feed_query_count; ++$i )
		{
			$feed_query = $this->build_feed_query_object( $request_data, $i, $listing );

			if ( $feed_query->feed_id != -1 || $feed_query->query_id != -1 )
			{
				$listing->feed_query_list[] = $feed_query;
			}
		}

		return $listing;
	}

	private function get_feed_query_count( $request_data )
	{
		return isset( $request_data[ 'listing_feed_query_count' ] ) ? (int) $request_data[ 'listing_feed_query_count' ] : 0;
	}

	private function build_listing_object( $request_data )
	{
		$map = array(
			'listing_id' => new Flexical_Admin_Object_Property_Spec( 'id', 'int' ),
			'listing_title' => new Flexical_Admin_Object_Property_Spec( 'title', 'string' ),
			'listing_format_id' => new Flexical_Admin_Object_Property_Spec( 'format_id', 'int' ),
			'listing_shortcode_key' => new Flexical_Admin_Object_Property_Spec( 'shortcode_key', 'string' ),
			'listing_sort_order' => new Flexical_Admin_Object_Property_Spec( 'sort_order', 'string' ),
			'listing_max_number_of_events' => new Flexical_Admin_Object_Property_Spec( 'max_number_of_events', 'int' ),
		);

		$info = new MVC_Dynamo_Info( 'flexical/model/listing.php', 'Flexical_Model_Listing' );

		$listing_object_builder = new Flexical_Admin_Object_Builder( $map, $info );

		$object = $listing_object_builder->build( $request_data );

		if ( isset( $object->sort_order) && $object->sort_order != 'ascending' && $object->sort_order != 'descending' )
		{
			throw new Exception( "Invalid listing sort order: " . $object->sort_order );
		}

		return $object;
	}

	private function build_feed_query_object( $request_data, $num, $listing )
	{
		$feed_query = new Flexical_Model_Feed_Query();

		$feed_query->feed_id  = $request_data[ 'listing_feed_' . $num ];
		$feed_query->query_id = $request_data[ 'listing_query_' . $num ];
		$feed_query->listing_id = $listing->id;

		return $feed_query;
	}
}

?>